package com.markerhub.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.Blog;
import com.markerhub.entity.Weibo;
import com.markerhub.service.BlogService;
import com.markerhub.service.WeiboService;
import com.markerhub.service.impl.WeiboServiceImpl;
import com.markerhub.util.ShiroUtil;
import org.apache.logging.log4j.message.ReusableMessage;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import weibo4j.model.Status;
import weibo4j.org.json.JSONException;
import weibo4j.org.json.JSONObject;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 *
 *
 */
@CrossOrigin
@RestController
public class BlogController {

    @Autowired
    WeiboService weiboService;


//
//    @GetMapping("/blogs")
//    public  Result Nlist()  {
//        WeiboService weiboService=new WeiboServiceImpl();
//
//
//
//
//    return Result.succ(200,"success",   weiboService.ViewWeibo());
//
//    }


    @GetMapping("/blogs")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage, HttpServletResponse response) {

        Page page = new Page(currentPage, 5);
        IPage pageData = weiboService.page(page, new QueryWrapper<Weibo>().orderByDesc("date"));
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        return Result.succ(pageData);
    }

    @GetMapping("/blog/{id}")
    public Result detail(@PathVariable(name = "id") Long id) {
        Weibo weibo = weiboService.getById(id);
        Assert.notNull(weibo, "该博客已被删除");

        return Result.succ(weibo);
    }

//    @RequiresAuthentication
//    @PostMapping("/blog/edit")
//    public Result edit(@Validated @RequestBody Blog blog) {
//
////        Assert.isTrue(false, "公开版不能任意编辑！");
//
//        Weibo temp = null;
//        if(blog.getId() != null) {
//            temp = weiboService.getById(blog.getId());
//            // 只能编辑自己的文章
//            System.out.println(ShiroUtil.getProfile().getId());
////            Assert.isTrue(temp.getUserId().longValue() == ShiroUtil.getProfile().getId().longValue(), "没有权限编辑");
//
//        } else {
//
////            temp = new Weibo();
////            temp.setUserId(ShiroUtil.getProfile().getId());
////            temp.setCreated(LocalDateTime.now());
////            temp.setStatus(0);
//        }
//
//        BeanUtil.copyProperties(blog, temp, "id", "userId", "created", "status");
//        weiboService.saveOrUpdate(temp);
//
//        return Result.succ(null);
//    }


}
